home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Library / arexx.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  133 lines

  1. /*
  2.  * arexx.c  V3.1
  3.  *
  4.  * ToolManager library ARexx handling routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Send an ARexx command */
  19. #undef  DEBUGFUNCTION
  20. #define DEBUGFUNCTION SendARexxCommand
  21. BOOL SendARexxCommand(const char *command, ULONG len)
  22. {
  23.  struct Library *RexxSysBase;
  24.  BOOL            rc          = FALSE;
  25.  
  26.  AREXX_LOG(LOG2(Arguments, "Cmd '%s' (%ld)", command, len))
  27.  
  28.  /* Open ARexx system library */
  29.  if (RexxSysBase = OpenLibrary(RXSNAME, 0)) {
  30.   struct MsgPort *mp;
  31.  
  32.   AREXX_LOG(LOG1(RexxSysBase, "0x%08lx", RexxSysBase))
  33.  
  34.   /* Allocate message port */
  35.   if (mp = CreateMsgPort()) {
  36.    struct RexxMsg *rxmsg;
  37.  
  38.    AREXX_LOG(LOG1(Reply port, "0x%08lx", mp))
  39.  
  40.    /* Allocate ARexx message */
  41.    if (rxmsg = CreateRexxMsg(mp, NULL, NULL)) {
  42.  
  43.     AREXX_LOG(LOG1(ARexx Msg, "0x%08lx", rxmsg))
  44.  
  45.     /* Create ARexx argument */
  46.     if (rxmsg->rm_Args[0] = CreateArgstring(command, len)) {
  47.      struct MsgPort *ap;
  48.  
  49.      AREXX_LOG(LOG0(Argument created))
  50.  
  51.      /* Initialize ARexx message */
  52.      rxmsg->rm_Action = RXCOMM | RXFF_NOIO;
  53.  
  54.      /* Find port and send message */
  55.      Forbid();
  56.      if (ap = FindPort("AREXX")) PutMsg(ap, (struct Message *) rxmsg);
  57.      Permit();
  58.  
  59.      /* Success? */
  60.      if (ap) {
  61.  
  62.       AREXX_LOG(LOG1(AREXX port, "0x%08lx", ap))
  63.  
  64.       /* Yes, wait on reply and remove it */
  65.       WaitPort(mp);
  66.       GetMsg(mp);
  67.  
  68.       /* Check return code */
  69.       rc = (rxmsg->rm_Result1 == RC_OK);
  70.      }
  71.  
  72.      ClearRexxMsg(rxmsg, 1);
  73.     }
  74.  
  75.     DeleteRexxMsg(rxmsg);
  76.    }
  77.  
  78.    DeleteMsgPort(mp);
  79.   }
  80.  
  81.   CloseLibrary(RexxSysBase);
  82.  }
  83.  
  84.  AREXX_LOG(LOG1(result, "%ld", rc))
  85.  
  86.  return(rc);
  87. }
  88.  
  89. /* Start an ARexx program */
  90. #undef  DEBUGFUNCTION
  91. #define DEBUGFUNCTION StartARexxProgram
  92. BOOL StartARexxProgram(const char *cmd, const char *cdir,
  93.                        struct AppMessage *msg)
  94. {
  95.  BPTR newcd;
  96.  BOOL rc    = FALSE;
  97.  
  98.  AREXX_LOG(LOG3(Arguments, "Cmd '%s' Dir '%s' Msg 0x%08lx", cmd, cdir, msg))
  99.  
  100.  /* Lock current directory */
  101.  if (newcd = Lock(cdir, SHARED_LOCK)) {
  102.   char  *cmdline;
  103.   ULONG  length;
  104.  
  105.   AREXX_LOG(LOG1(NewCD, "0x%08lx", newcd))
  106.  
  107.   /* Build command line */
  108.   if (cmdline = BuildCommandLine(cmd, msg, newcd, &length)) {
  109.    BPTR oldcd;
  110.  
  111.    AREXX_LOG(LOG3(cmdline, "'%s' (0x%08lx, %ld)", cmdline, cmdline, length))
  112.  
  113.    /* Go to program's current directory */
  114.    oldcd = CurrentDir(newcd);
  115.  
  116.    /* Send ARexx command */
  117.    rc = SendARexxCommand(cmdline, length);
  118.  
  119.    /* Go back to old current directory */
  120.    CurrentDir(oldcd);
  121.  
  122.    /* Free command line */
  123.    FreeVector(cmdline);
  124.   }
  125.  
  126.   UnLock(newcd);
  127.  }
  128.  
  129.  AREXX_LOG(LOG1(Result, "%ld", rc))
  130.  
  131.  return(rc);
  132. }
  133.